package cn.xhc.question.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import cn.xhc.common.util.ContantInfoUtil;
import cn.xhc.common.util.MutilThreadUtil;
import cn.xhc.common.util.YxlSpiderUtil;
import cn.xhc.question.pojo.AColumn;
import cn.xhc.question.pojo.AItem;
import cn.xhc.question.pojo.CommonEnum;
import cn.xhc.question.service.IQuestionService;
import cn.xhc.question.service.IQuestionSpiderService;
@Service("questionSpiderService")
public class QuestionSpiderServiceImpl implements IQuestionSpiderService{
	/**
	 * 抓测试项信息
	 */
	@Resource
	private IQuestionService questionService;
	/**
	 * 1.查询要添加数据的栏目,如url为null-->查询所有栏目
	 * 2.查询栏目的所有测试项
	 * 3.将数据插入数据库
	 */
	public JSONObject saveBatchItemsServiceBcak(String url) {
		ContantInfoUtil.LOGGER.info("------saveBatchItemsService------");
		JSONObject resultJson = new JSONObject();
		List<AColumn> columnsList = new ArrayList<AColumn>();
		Map<String, Object> condMap = new HashMap<String, Object>();
		/*判断url是否传递*/
		if(url == null || "".equalsIgnoreCase(url)){
			/*没有传递url*/
			/*查询所有启用的栏目*/
			condMap.put("status",CommonEnum.STATUS_ENABLED.getFlag());
			columnsList = this.questionService.findCondListColumnService(condMap);
		}else{
			/*传递了url*/
			condMap.clear();
			/*根据url查询栏目*/
			condMap.put("url",url);
			AColumn column = this.questionService.findOneColumnService(condMap);
			columnsList.add(column);
		}
		/*查询栏目下对应的测试项*/
		for (Iterator iterator = columnsList.iterator(); iterator.hasNext();) {
			AColumn column = (AColumn) iterator.next();
			ContantInfoUtil.LOGGER.info("正在抓取栏目:name:{},url:{}",column.getName(),column.getUrl());
			/*抓取测试项数据*/
			JSONObject itemsJson = YxlSpiderUtil.itemList(column.getUrl(), 1);
			/*分页采集数据*/
			/*第一次只查询总页数*/
			if("0".equalsIgnoreCase(itemsJson.getString("code"))){
				/*数据抓取成功,正常采集*/
				JSONObject dataJson = itemsJson.getJSONObject("data");
				int totalPage = itemsJson.getIntValue("totalPage");
				/*分页采集数据*/
				for(int i = 1;i <= 1; i++){
					ContantInfoUtil.LOGGER.info("正在抓取栏目:name:{},url:{},总页数:{}",column.getName(),column.getUrl(),totalPage);
					/*采集当前页的数据*/
					itemsJson = YxlSpiderUtil.itemList(column.getUrl(), i);
					if("0".equalsIgnoreCase(itemsJson.getString("code"))){
						/*采集成功*/
						dataJson = itemsJson.getJSONObject("data");
						JSONArray itemsArr = dataJson.getJSONArray("itemsList");
						for (Iterator iterator2 = itemsArr.iterator(); iterator2.hasNext();) {
							JSONObject jsonTemp = (JSONObject) iterator2.next();
							/*获取当前数据的id*/
							int outId = jsonTemp.getIntValue("outId");
							/*根据outId查询item*/
							condMap.clear();
							condMap.put("outId",outId);
							AItem item = this.questionService.findOneItemService(condMap);
							/*假设数据库已经存在该数据*/
							boolean update = true;
							if(item == null){
								/*数据库不存在该数据*/
								item = new AItem();
								item.setCreateTime(new Date());
								update = false;
							}
							item.setColumnsId(column.getId());
							item.setContent("");
//							item.setContent(jsonTemp.getString("content"));
							item.setImgPath(jsonTemp.getString("imgPath"));
							item.setName(jsonTemp.getString("name"));
							item.setOutId(outId);
							item.setQuestionNum(jsonTemp.getIntValue("questionNum"));
							item.setUpdateTime(new Date());
							item.setPubTime(new Date());
							item.setStatus((byte) 0);
							/*outId不存在-->保存;存在-->更新*/
							if(update){
								/*更新*/
								resultJson = this.questionService.updateOneItemService(item);
							}else{
								/*保存*/
								resultJson = this.questionService.saveOneItemService(item);
							}
							
						}
					}
					
				}
			}
		}
		return resultJson;
	}
	/**
	 * 多线程
	 */
	@Override
	public JSONObject saveBatchItemsService(String url) {
		ContantInfoUtil.LOGGER.info("------saveBatchItemsService------");
		JSONObject resultJson = new JSONObject();
		List<AColumn> columnsList = new ArrayList<AColumn>();
		Map<String, Object> condMap = new HashMap<String, Object>();
		/*判断url是否传递*/
		if(url == null || "".equalsIgnoreCase(url)){
			/*没有传递url*/
			/*查询所有启用的栏目*/
			condMap.put("status",CommonEnum.STATUS_ENABLED.getFlag());
			columnsList = this.questionService.findCondListColumnService(condMap);
			/*查询栏目下对应的测试项*/
			List<MutilThreadUtil>  threadList = new ArrayList<MutilThreadUtil>();
			for (Iterator iterator = columnsList.iterator(); iterator.hasNext();) {
				AColumn column = (AColumn) iterator.next();
//				ContantInfoUtil.LOGGER.info("正在抓取栏目:name:{},url:{}",column.getName(),column.getUrl());
				/*创建线程*/
				MutilThreadUtil mutilThreadUtil = new MutilThreadUtil();
				Map<String, Object> paramsMap = new HashMap<String, Object>();
				paramsMap.put("operType","spiderColumn");
				paramsMap.put("url",column.getUrl());
				paramsMap.put("questionSpiderService",this);
				mutilThreadUtil.setParamsMap(paramsMap);
				threadList.add(mutilThreadUtil);
			}
			ExecutorService executorService = Executors.newFixedThreadPool(threadList.size());
			try {
				/*启动线程*/
				executorService.invokeAll(threadList);
				ContantInfoUtil.LOGGER.info("启动线程:{}",executorService);
			} catch (InterruptedException e) {
				ContantInfoUtil.LOGGER.info("执行多线程出错:{}",e);
			}finally{
				/*关闭线程*/
				executorService.shutdown();
			}
		}else{
			/*传递了url*/
			condMap.clear();
			/*根据url查询栏目*/
			condMap.put("url",url);
			AColumn column = this.questionService.findOneColumnService(condMap);
			resultJson = this.spiderColumn(column);
		}
		
		return resultJson;
	}
	/**
	 * 抓取指定的栏目
	 * @param column
	 */
	private JSONObject spiderColumn(AColumn column) {
		Map<String, Object> condMap = new HashMap<String, Object>();
		JSONObject resultJson = new JSONObject();
//		ContantInfoUtil.LOGGER.info("正在抓取栏目:name:{},url:{}",column.getName(),column.getUrl());
		/*抓取测试项数据*/
		JSONObject itemsJson = YxlSpiderUtil.itemList(column.getUrl(), 1);
		/*分页采集数据*/
		/*第一次只查询总页数*/
		if("0".equalsIgnoreCase(itemsJson.getString("code"))){
			/*数据抓取成功,正常采集*/
			JSONObject dataJson = itemsJson.getJSONObject("data");
			int totalPage = dataJson.getIntValue("totalPage");
			ContantInfoUtil.LOGGER.info("总页数:{}",totalPage);
			/*分页采集数据*/
			for(int i = 1;i <= totalPage; i++){
				ContantInfoUtil.LOGGER.info("总页数:{},正在抓取栏目:name:{},url:{},",totalPage,column.getName(),column.getUrl());
				/*采集当前页的数据*/
				itemsJson = YxlSpiderUtil.itemList(column.getUrl(), i);
				if("0".equalsIgnoreCase(itemsJson.getString("code"))){
					/*采集成功*/
					dataJson = itemsJson.getJSONObject("data");
					JSONArray itemsArr = dataJson.getJSONArray("itemsList");
					for (Iterator iterator2 = itemsArr.iterator(); iterator2.hasNext();) {
						JSONObject jsonTemp = (JSONObject) iterator2.next();
						/*获取当前数据的id*/
						int outId = jsonTemp.getIntValue("outId");
						/*根据outId查询item*/
						condMap.clear();
						condMap.put("outId",outId);
						AItem item = this.questionService.findOneItemService(condMap);
						/*假设数据库已经存在该数据*/
						boolean update = true;
						if(item == null){
							/*数据库不存在该数据*/
							item = new AItem();
							item.setCreateTime(new Date());
							update = false;
						}
						item.setColumnsId(column.getId());
						item.setContent("");
//						item.setContent(jsonTemp.getString("content"));
						item.setImgPath(jsonTemp.getString("imgPath"));
						item.setName(jsonTemp.getString("name"));
						item.setOutId(outId);
						item.setQuestionNum(jsonTemp.getIntValue("questionNum"));
						item.setUpdateTime(new Date());
						item.setPubTime(new Date());
						item.setStatus((byte) 0);
						/*outId不存在-->保存;存在-->更新*/
						if(update){
							/*更新*/
							resultJson = this.questionService.updateOneItemService(item);
						}else{
							/*保存*/
							resultJson = this.questionService.saveOneItemService(item);
						}
						
					}
				}
				
			}
		}
		return resultJson;
	}
	
}
